
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author enrico
 */
public class PrestitiView extends BorderPane {
  
  PrestitiController controller;
  TableView<PrestitiModel> tv = new TableView<PrestitiModel>();
  
  public PrestitiView(PrestitiController pc) {
    controller = pc;
    TableColumn titolo = new TableColumn("Titolo");
    titolo.setCellValueFactory(new PropertyValueFactory<PrestitiModel, String>("titolo"));
    TableColumn nome = new TableColumn("Lettore");
    nome.setCellValueFactory(new PropertyValueFactory<PrestitiModel, String>("persona"));
    TableColumn data = new TableColumn("Data prestito");
    data.setCellValueFactory(new PropertyValueFactory<PrestitiModel, String>("data"));
    TableColumn giorni = new TableColumn("Giorni");
    giorni.setCellValueFactory(new PropertyValueFactory<PrestitiModel, String>("giorni"));
    TableColumn ritorno = new TableColumn("Ritorno");
    ritorno.setCellValueFactory(new PropertyValueFactory<PrestitiModel, String>("ritorno"));
    
    titolo.setPrefWidth(150);
    nome.setPrefWidth(150);
    data.setPrefWidth(100);
    giorni.setPrefWidth(100);
    
    tv.getColumns().addAll(titolo,nome,data,ritorno,giorni);
    
    HBox hb = new HBox();
    Button ritorna = new Button("Segna il prestito come terminato");
    ritorna.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent arg0) {
        PrestitiModel pm = tv.getSelectionModel().getSelectedItem();
        controller.togliPrestito(pm);
        tv.getItems().remove(pm);
      }
      
    });
    Button stampa = new Button("Stampa prestito");
    stampa.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent arg0) {
        controller.stampaPrestito();
      }
    });
    hb.getChildren().addAll(ritorna,stampa);
    
    this.setCenter(tv);
    this.setBottom(hb);
    this.setMargin(hb, new Insets(10));
  }
  
  public TableView<PrestitiModel> getTV() {
    return tv;
  }

  PrestitiModel getSelectedModelPrestito() {
    return tv.getSelectionModel().getSelectedItem();
  }
}
